<!-- 样品接收 -->
<template>
  <div>
    <div class="searchbox">
      <el-form :inline="true" size="small" label-width="100px">
        <jt-easy-collapse :has-expand="true">
          <template slot="expand">
                                    <el-form-item label="任务单号">
                                          <el-input size="small" v-model="form.taskNo" placeholder="任务单号"></el-input>
                                        </el-form-item>
                                        <el-form-item label="零件名称">
                                          <el-input size="small" v-model="form.partName" placeholder="零件名称"></el-input>
                                        </el-form-item>
                                        <el-form-item label="零件号">
                                          <el-input size="small" v-model="form.partNumber" placeholder="零件号"></el-input>
                                        </el-form-item>

                                        <el-form-item label="零件图号">
                                          <el-input size="small" v-model="form.partGraphNumber" placeholder="零件图号"></el-input>
                                        </el-form-item>
                                        <el-form-item label="样品阶段">
                                          <!-- <el-input size="small" v-model="form.sampleStatusPhase" placeholder="样品阶段"></el-input> -->
                                          <el-select v-model="form.sampleStatusPhase" placeholder="请选择">
                                           <el-option  :label="all" :value="''"></el-option>
                                               <el-option v-for="item in sampleStatusPhaseList" :key="item.code" :label="item.name" :value="item.code">
                                              </el-option>
                                          </el-select>
                                        </el-form-item>
                                        <el-form-item label="项目号">
                                          <el-input size="small" v-model="form.projectNo" placeholder="项目号"></el-input>
                                        </el-form-item>
                                        <el-form-item label="报验批次信息">
                                          <el-input size="small" v-model="form.batchNumber" placeholder="报验批次信息"></el-input>
                                        </el-form-item>
                                        <el-form-item label="材料牌号">
                                          <el-input size="small" v-model="form.materialNo" placeholder="材料牌号"></el-input>
                                        </el-form-item>
                                             <el-form-item label="热处理炉号">
                                          <el-input size="small" placeholder="热处理炉号" v-model='form.heatingNumber'></el-input>
                                        </el-form-item>
                                    <el-form-item style="margin-left: 20px;">
                                      <el-button size="mini" type="primary" @click="refreshData()">查询</el-button>
                                    </el-form-item>
                                    <el-form-item>
                                      <el-button size="mini" @click='clearForm'>清除</el-button>
                                    </el-form-item>
</template>
        </jt-easy-collapse>
      </el-form>
    </div>
    <div class="contentbox">
      <div style="float: left;padding-left: 20px;font-size: 15px;color: #666;padding-top: 6px;">
        委托单信息 共 <span style="color: red;">{{totalElements}}</span> 条数据
      </div>
      <el-row style="float: right;margin-right: 20px;margin-bottom: 15px;" type="flex" justify="end">
        <el-col>
          <jt-table-column-filter :table-key.sync="tableKey" :header-data.sync="tableHeaderArr" tableName='sampleReception' :filter-column.sync="filterColumnData"></jt-table-column-filter>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table style="width: 100%;" :data="tableData" :key='tableKey' v-loading="loading" @selection-change="handleSelectionChange"
        border stripe>
        <!-- <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="序号" align="center"></el-table-column>
        <el-table-column prop="createTime" label="创建日期" align="center"></el-table-column>
        <el-table-column prop="taskNo" label="任务单号" align="center"></el-table-column>
        <el-table-column prop="applyType" label="任务单类型" align="center"></el-table-column>
        <el-table-column prop="partNumber" label="零件号" align="center"></el-table-column>
        <el-table-column prop="quantity" label="零件数量" align="center"></el-table-column>
        <el-table-column prop="materialFurnanceNumber" label="热处理炉号" align="center"></el-table-column>
        <el-table-column prop="sampleStatusPhase" label="样品信息" align="center"></el-table-column>
        <el-table-column prop="applyUser.name" label="送样人" align="center"></el-table-column>
        <el-table-column prop="projectNo" label="项目号" align="center"></el-table-column>
        <el-table-column prop="sampleStatusPhase" label="样品阶段" align="center"></el-table-column>
        <el-table-column prop="materialNo" label="材料牌号" align="center"></el-table-column>
        <el-table-column prop="batchNumber" label="报验批次信息" align="center"></el-table-column> -->
        <el-table-column align="center" :key='item' v-for='item in filterColumnData' :resizable="false" :label="getTableHeaderLabel(item)"
          :prop="item" :min-width="getTableColumnWidth(item)" :fixed="getTableColumnFixed(item)" :sortable="getTableColumnSort(item)">
<template slot-scope="scope">
   {{scope.row[item]}}
  <el-button v-if="item === 'operate'" size="small" type="text" @click="acceptSample(scope.row)">接收样品</el-button>
</template>
        </el-table-column>
      </el-table>

      <el-row type="flex" justify="center">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="pageSizeArr"
          :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalElements"></el-pagination>
      </el-row>
    </div>
  </div>
</template>

<script>
  import {
    getTaskSampleListService,
    postTakeSampleService,
    getSampleStatusPhase,
    getSampleStatusDrop,
  } from 'service/task';

  import {
    APPLY_TYPE_LIST
  } from 'service/constant';
  import {
    getLableFromList
  } from 'utils/common';
  import JtTableColumnFilter from 'components/jt-table-column-filter/index'; // 导入表格列筛选组件
  import JtEasyCollapse from 'components/jt-easy-collapse/index';
  export default {
    name: 'SampleReception',
    components: {
      'jt-table-column-filter': JtTableColumnFilter,
      'jt-easy-collapse': JtEasyCollapse,
    },
    data() {
      return {
        status: '',
        sampleStatusPhaseList: [],
        sampleStatusDropList:[],
        tableKey: 1, // table key
        filterColumnData: [],
        pageSizeArr: [10, 20, 50, 100],
        form: {},
        all:'全部',
        tableHeaderArr: [{
            'name': 'createTime',
            'alias': '创建日期',
            'default': true,
            'width': 170,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'taskNo',
            'alias': '任务单号',
            'default': true,
            'width': 170,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'applyTypeStr',
            'alias': '任务类型',
            'default': true,
            'width': 170,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'partNumber',
            'alias': '零件号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'quantity',
            'alias': '样品数量',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'heatingNumber',
            'alias': '热处理炉号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'sampleStatu',
            'alias': '样品信息',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'sendUserName',
            'alias': '送样人',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'projectNo',
            'alias': '项目号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'sampleStatusPhaseStr',
            'alias': '样品阶段',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'materialNo',
            'alias': '材料牌号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'batchNumber',
            'alias': '报验批次信息',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'operate',
            'alias': '操作',
            'default': true,
            'width': 190,
            'fixed': 'right',
            'sort': false
          }
        ],
        loading: false,
        tableData: [],
        totalElements: 0,
        pageSize: 10,
        pageNumber: 1,
        currentPage: 1,
        sort: '',
        order: ''
      }
    },
    methods: {
       // 清除
      clearForm() {
        this.form = {};
        this.refreshData();
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.refreshData();
      },
      handleCurrentChange(val) {
        this.pageNumber = val;
        this.refreshData();
      },
      refreshData() {

        getTaskSampleListService({ ...this.form,
            pageNumber: this.pageNumber,
            pageSize: this.pageSize,
            sort: this.sort,
            order: this.order
          })
          .then(response => {
            this.tableData = response['data']['result']['content'];
            this.totalElements = response['data']['result']['totalElements'];
            // this.currentPage = response.data.result.pageable.pageNumber;
            this.pageSize = response.data.result.pageable.pageSize;
            if (this.tableData) {
              for (let i = 0; i < this.tableData.length; i++) {
                this.tableData[i].sampleStatu = getLableFromList(this.sampleStatusDropList, this.tableData[i].sampleStatus);
               this.tableData[i].applyTypeStr = getLableFromList(APPLY_TYPE_LIST, this.tableData[i].taskType);
                this.tableData[i].sampleStatusPhaseStr = getLableFromList(this.sampleStatusPhaseList, this.tableData[i].sampleStatusPhase);
                this.tableData[i].sendUserName = this.tableData[i].sendUser ? this.tableData[i].sendUser.name : '';
              }
            }
          })
      },

      acceptSample(row) {
        this.$confirm('是否接收此样品?', '提示', {
          type: 'warning'
        }).then(() => {
          postTakeSampleService(row.id)
            .then(res => {
              if (res.data.success) {
                this.$message({
            message: `已接收的样品对应任务单号为：${row.taskNo}`,
            type: 'success',
            duration: 1000
            });
                this.refreshData();
              } else {
                this.$alert(res.data.message, '提示', {
                  type: 'error'
                })
              }
            })
        }).catch(() => {});
      },
      // 获取表格多选的值
      handleSelectionChange(val) {},
      // 表头展示
      getTableHeaderLabel(str) {
        return this.getTableSomeValue(str, 'alias');
      },
      // 获取表格列的宽度
      getTableColumnWidth(str) {
        return this.getTableSomeValue(str, 'width');
      },
      // 获取表格列是否是固定的
      getTableColumnFixed(str) {
        return this.getTableSomeValue(str, 'fixed');
      },
      // 获取表格列是否排序
      getTableColumnSort(str) {
        return this.getTableSomeValue(str, 'sort');
      },
      // 获取表格表头，列宽度，列固定，列排序的值
      getTableSomeValue(str, keyName) {
        for (var value of this.tableHeaderArr) {
          if (value['name'] == str) {
            return value[keyName];
          }
        }
      }
    },
    mounted() {
      getSampleStatusPhase().then(res => {
        this.sampleStatusPhaseList = res.data.result;
      });
         getSampleStatusDrop().then(res => {
        this.sampleStatusDropList = res.data.result;
      });
      this.refreshData();
    },
  }
</script>
